/*
 * This takes all the input private message parameters from the front end
 * and has them input into the database to be retrieved by by the receiver
 */

package airport.forum.action;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import airport.forum.dao.PrivateMessageDAO;
import airport.forum.pojo.PrivateMessage;
import airport.user.dao.UserDAO;

public class PrivateMessageAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware {
	private static final long serialVersionUID = 1L;
	
	//gotten from cookie
	private String sender;
	// following three are gotten from user input via jsp page
	private String receiver;
	private String title;
	private String body;
	//generated automatically to be when object is created
	private Date dateCreated;

	protected HttpServletResponse servletResponse;
	
	
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	protected HttpServletRequest servletRequest;

	
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String execute() {
		
		//zero constructor for date sets date to be right now, as object is being created.
		dateCreated = new Date();

		String ret = "success";
		PrivateMessage pm = new PrivateMessage();
		System.out.println("Body:"+body);
		System.out.println("Title:"+title);
		System.out.println("receiver:"+receiver);
		pm.setTitle(title);
		pm.setBody(body);
		pm.setSender(sender);
		pm.setReceiver(receiver);
		pm.setDateCreated(dateCreated);
		pm.setRead(false);
		PrivateMessageDAO dao = new PrivateMessageDAO();
		dao.addPrivateMessage(pm);
		return ret;

	}

	public void validate() {
		
		//get sender's information from cookie to pass onto PM object in Database
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if (cookieName.equals(cookie.getName())) {
					setSender(cookie.getValue());
					break;
				}
			}
			
			if (title.length() > 65 || title.length() < 5){
				addFieldError("error",
						"Thread Title must be less that 65 characthers and more than 4 characters");
			}
			//the length of opening post >3 && < 1000
			if (body.trim().length() < 3 || body.trim().length() > 1000){
				addFieldError("error",
						"Opening post must be longer than 3 characters and shorter than 1000 characters");
				
			}
		}
		
		//get receiver's info to make sure they exist
		UserDAO dao = new UserDAO();
		if(dao.findUserByUserName(receiver) == null) {
			addFieldError("receiver", "Receiver username doesn't exist");
		}
		else//check message title
			if(title.length() > 65) {
			//title = "(no subject)";
				addFieldError("receiver", "Title should not exceed 45 characters");
			}//check message length (>3 chars)
			else
				if((body.length() < 3)||(body.length()>1000)) {
					addFieldError("receiver", "Invalid Message Length (3 < length < 1000)");
				}
		if (title.length() == 0)
			title = "(no subject)";
	
	}


	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}
}
